package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.CarBo;
import com.groupseven.happycampus.common.pojo.Car;
import com.groupseven.happycampus.common.result.LayuiPageVo;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.vo.PieVo;
import com.groupseven.happycampus.mapper.CarMapper;
import com.groupseven.happycampus.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.security.auth.Subject;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("carService")
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;

    @Override
    public LayuiPageVo<Car> carPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page, limit);

        List<Car> cars = carMapper.selectByParams(params);

        // mybatis 的 分页数据结构
        PageInfo<Car> carPageInfo = new PageInfo<>(cars);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<Car> carLayuiPageVo = new LayuiPageVo<>();
        carLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        carLayuiPageVo.setMsg("分页列表数据"); // 提示信息


        carLayuiPageVo.setCount((int) carPageInfo.getTotal()); // 总记录数


        carLayuiPageVo.setData(carPageInfo.getList());  // 分页的列表数据
        return carLayuiPageVo;
    }

    @Override
    public int deleteCar(Integer id) {
        return carMapper.deleteCar(id);
    }

    @Override
    public int deleteCarsByIds(String ids) {
        String[] arrId = ids.split(",");
        return carMapper.deleteCarsByIds(arrId);
    }

    @Override
    public int addCar(Car car) {
        int i=carMapper.addCar(car);
        return i;
    }

    @Override
    public int updateCar(Map<String, Object> params) {
        int i=carMapper.updateCar(params);
        return i;
    }

    @Override
    public List<CarBo> carExcelData(Car car) {
        // 从数据库查询出来的 是  po  实体类
        List<Car> cars = carMapper.selectCar(car);
        // 要把 po 转为 bo
        List<CarBo> carBos = cars.stream()
                .map((cr) -> {
                    CarBo bo = new CarBo();
                    bo.setId(cr.getId());
                    bo.setCard(cr.getCard());
                    bo.setType(cr.getType());
                    bo.setTime(cr.getTime());
                    bo.setPhone(cr.getPhone());
                    bo.setName(cr.getName());
                    bo.setRmb(cr.getRmb());
                    bo.setPlace(cr.getPlace());
                    bo.setOrdernum(cr.getOrdernum());
                    return bo;
                }).collect(Collectors.toList());

        return carBos;
    }

    @Override
    public List<PieVo> echartsPie() {
        List<PieVo> list = carMapper.echartsPie();
        return list;
    }
}
